package edu.csumb.itcd.teammario;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class LeaderBoard {
    
    private static int killCount = 0;
    private Connection connect = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet resultSet = null;
	static final String URL = "jdbc:mysql://mlc104/curr4581";
	static final String USER = "curr4581";
	static final String PASSWORD = "Qu7hZ7SmP9XuWar5";
	static final String DRIVER = "com.mysql.jdbc.Driver";
	private int previousShotsFired;
	private int previousShotsLanded;
	private int previousKillCount;
	private static String username;
	private static String userPassword;

    public static int getKillCount() {
        return killCount;
    }

    public static void incrementKillCount() {
        killCount ++;
        return;
    }
    
    public static void setUsername(String input)
    {
    	username = input;
    }
    
    public static void setPassword(String input)
    {
    	userPassword = input;
    }
    
    
    public void signup(String name, String pass) throws SQLException {
		try {
			username = name;
	    	userPassword = pass;
			// This will load the MySQL driver, each DB has its own driver
			Class.forName(DRIVER);
			// Setup the connection with the DB
			connect = DriverManager.getConnection(URL, USER, PASSWORD);

			System.out.println("1");
			// Statements allow to issue SQL queries to the database
			statement = connect.createStatement();
			System.out.println("2");
			resultSet = statement.executeQuery("select * from curr4581.customer");
			System.out.println(resultSet.getInt(1) + "3");
			System.out.println("4");
			if (resultSet == null)
	    	{
	    		statement.executeQuery("insert into curr4581-Game(UserName, Password) values(\"" + username + "\", \"" + userPassword + "\"");
	    		
	    	}
			
//			// Result set get the result of the SQL query
//			resultSet = statement.executeQuery("select * from curr4581.Player where Name=\"Lyndon\"");
//			writeResultSet(resultSet);
//
//			// PreparedStatements can use variables and are more efficient
//			preparedStatement = connect
//					.prepareStatement("UPDATE Player SET `Overall Shots Fired`=?, `Overall Shots Landed`=?, `Overall Accuracy`=?, `Overall Kill Count`=? WHERE Name=\"Lyndon\"");
//			// "myuser, webpage, datum, summery, COMMENTS from FEEDBACK.COMMENTS");
//			// Parameters start with 1
//			preparedStatement.setInt(1, previousShotsFired + Player.getShotsFired());
//			preparedStatement.setInt(2, previousShotsLanded + Player.getShotsLanded());
//			preparedStatement.setDouble(3, ((double)previousShotsLanded + (double)Player.getShotsLanded())/((double)previousShotsFired + (double)Player.getShotsFired()));
//			preparedStatement.setInt(4, previousKillCount + killCount);
//			preparedStatement.executeUpdate();
//
//			preparedStatement = connect
//					.prepareStatement("SELECT * from curr4581.Player");
//			resultSet = preparedStatement.executeQuery();
//			writeResultSet(resultSet);
//
////			// Remove again the insert comment
////			preparedStatement = connect
////			.prepareStatement("delete from FEEDBACK.COMMENTS where myuser= ? ; ");
////			preparedStatement.setString(1, "Test");
////			preparedStatement.executeUpdate();
////			
////			resultSet = statement
////			.executeQuery("select * from FEEDBACK.COMMENTS");
////			writeMetaData(resultSet);
//			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally
		{
			System.out.println("5");
			close();
		}
	}
    
    private void writeMetaData(ResultSet resultSet) throws SQLException {
		// 	Now get some metadata from the database
		// Result set get the result of the SQL query
		
		System.out.println("The columns in the table are: ");
		
		System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
		for  (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
			System.out.println("Column " +i  + " "+ resultSet.getMetaData().getColumnName(i));
		}
	}

	private void writeResultSet(ResultSet resultSet) throws SQLException {
		// ResultSet is initially before the first data set
		while (resultSet.next()) {
			// It is possible to get the columns via name
			// also possible to get the columns via the column number
			// which starts at 1
			// e.g. resultSet.getSTring(2);
			System.out.println(resultSet.getInt(1));
			
		}
	}

	// You need to close the resultSet
	private void close() {
		try {
			if (resultSet != null) {
				resultSet.close();
			}

			if (statement != null) {
				statement.close();
			}

			if (connect != null) {
				connect.close();
			}
		} catch (Exception e) {

		}
	}
}

